// Copyright (C) 2010-2012, MaGUI
//
//  Elena Ballesteros Molinero
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Magui
{

    #region ComboBox de colores

    /// <summary>
    /// Turn a normal Combobox into a color picker.
    /// </summary>
    public class ColorComboAgent
    {
        private readonly Color[] colorArray;
        private bool SelectedIndexChangedEventEnabled;
        private ComboBox box;
        private Color currentColor;
        private bool useCustomColor;

        public ColorComboAgent(ComboBox comboBox)
        {
            colorArray = new Color[8];
            colorArray[0] = Color.White;
            colorArray[1] = Color.Black;
            colorArray[2] = Color.Red;
            colorArray[3] = Color.Blue;
            colorArray[4] = Color.Yellow;
            colorArray[5] = Color.Green;
            colorArray[6] = Color.Cyan;
            colorArray[7] = Color.Magenta;
            InitializeList(comboBox);
            SelectedIndexChangedEventEnabled = true;
        }

        /// <summary>
        /// Get and set color.
        /// </summary>
        public Color Color
        {
            get { return currentColor; }
            set
            {
                int dx = -1;

                currentColor = value;
                for (int i = 0; i < colorArray.Length; i++)
                {
                    if (colorArray[i] == value)
                    {
                        dx = i;
                        break;
                    }
                }

                useCustomColor = dx == -1;
                if (useCustomColor)
                {
                    colorArray[0] = currentColor;
                    dx = 0;
                }

                SelectedIndexChangedEventEnabled = false;
                box.SelectedIndex = dx;
                SelectedIndexChangedEventEnabled = true;

                box.Refresh(); //sometime there's delay, so force it to refresh
            }
        }

        public Color ColorPersonalizado { get; set; }

        private void InitializeList(ComboBox comboBox)
        {
            box = comboBox;
            box.DropDownStyle = ComboBoxStyle.DropDownList;
            box.DrawMode = DrawMode.OwnerDrawVariable;

            // Hook up the MeasureItem and DrawItem events
            box.DrawItem += DrawItemHandler;

            box.SelectedIndexChanged += box_SelectedIndexChanged;

            LoadColorsToBox();
        }


        private void LoadColorsToBox()
        {
            if (box.Items.Count < 2)
                box.DataSource = colorArray;
        }

        private void box_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!SelectedIndexChangedEventEnabled)
                return;

            //  if (box.SelectedIndex > 0)
            currentColor = colorArray[box.SelectedIndex];
            /*  else
            {
                using (System.Windows.Forms.ColorDialog fm = new System.Windows.Forms.ColorDialog())
                {
                    fm.Color = Color;
                    if (fm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Color = fm.Color;
                        colorPersonalizado = fm.Color;
                    }
                }
            }
           */
        }

        private void DrawItemHandler(object sender,
                                     DrawItemEventArgs e)
        {
            // Draw the background of the item.
            e.DrawBackground();

            // Draw color hint
            var colorRectangle = new Rectangle(2, e.Bounds.Top + 1,
                                               e.Bounds.Height*2, e.Bounds.Height - 2);
            e.Graphics.FillRectangle(new SolidBrush(colorArray[e.Index]), colorRectangle);

            // Draw string
            //        if (e.Index > 0)  // for named color
            //       {
            e.Graphics.DrawString(colorArray[e.Index].Name, box.Font, Brushes.Black,
                                  new RectangleF(e.Bounds.X + colorRectangle.Width, e.Bounds.Y, e.Bounds.Width,
                                                 e.Bounds.Height));
            //      }
            /*      else // for custom color
            {
                if (useCustomColor) // for assigned custom color
                    e.Graphics.DrawString("Personalizado", box.Font, System.Drawing.Brushes.Black,
                        new RectangleF(e.Bounds.X + colorRectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                else  // for not-yet assigned custom color
                    e.Graphics.DrawString("Personalizar...", box.Font, System.Drawing.Brushes.Black,
                        new RectangleF(e.Bounds.X + 2, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            }
*/
        }
    }

    #endregion
}